<template>
  <div class="app-container">
    <el-tabs>
      <el-tab-pane>
        <span slot="label"><i class="el-icon-house" /> 全部用例库</span>
        <!-- 搜索 -->
        <el-form :inline="true">
          <el-form-item label="用例库名称">
            <el-input v-model="q" placeholder="请输入用例库名称" clearable @keyup.enter.native="fetchData" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="fetchData">查询</el-button>
            <el-button type="primary" @click="addLib">新增用例库</el-button>
          </el-form-item>
        </el-form>
        <el-empty v-if="!libList.length" description="亲，先创建一个用例库吧~" />
        <CardList :list="libList" @edit="editLib" @delete="deleteLib" @follow="followLib" @goto="gotoCaselibDetail" />
        <!-- 分页 -->
        <pagination v-show="total > 0" :total="total" :page.sync="page" :limit.sync="limit" @pagination="fetchData" />
      </el-tab-pane>
      <el-tab-pane>
        <span slot="label"><i class="el-icon-star-off" /> 我关注的</span>
        <el-empty v-if="!followList.length" description="亲，关注的用例库在这里~" />
        <CardList :list="followList" @edit="editLib" @delete="deleteLib" @follow="followLib" @goto="gotoCaselibDetail" />
      </el-tab-pane>
    </el-tabs>

    <el-dialog :title="libDialogTitle" :visible.sync="libDialogShow" width="30%">
      <el-form ref="libForm" :model="libForm" :rules="libFormRules" label-width="90px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="libForm.name" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="libForm.desc" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="libDialogShow = false">取 消</el-button>
        <el-button type="primary" @click="saveSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import apiCaselib from '@/api/functional/caselib'
import CardList from './components/CardList.vue'
import Pagination from '@/components/Pagination'
const defaultForm = {
  id: '',
  name: '',
  desc: ''
}
export default {
  components: { CardList, Pagination },
  data() {
    return {
      libList: [],
      followList: [],
      q: '',
      page: 1,
      limit: 10,
      total: 0,
      listLoading: false,
      libDialogShow: false,
      libDialogTitle: '新增用例库',
      submitType: 0, // 0:新增 1:编辑
      libForm: defaultForm,
      libFormRules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ]
      }

    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const params = {
        page: this.page,
        limit: this.limit,
        name: this.q
      }
      apiCaselib.getCaselibList(params).then(response => {
        this.libList = response.data
        this.followList = response.follows
        this.total = response.count
      })
      this.listLoading = false
    },
    addLib() {
      this.libDialogShow = true
      this.libDialogTitle = '新增用例库'
      this.submitType = 0
      this.libForm = Object.assign({}, defaultForm)
    },
    editLib(item) {
      this.libDialogShow = true
      this.libDialogTitle = '编辑用例库'
      this.submitType = 1
      this.libForm = Object.assign({}, item)
    },
    async saveSubmit() {
      await this.$refs.libForm.validate(async validate => {
        if (validate) {
          if (this.submitType === 0) {
            await apiCaselib.createCaselib(this.libForm).then(response => {
              this.$message.success(response.message || '新增成功')
            }).catch(error => {
              this.$message.error(error.response.data['message'])
            })
          } else {
            await apiCaselib.updateCaselib(this.libForm).then(response => {
              this.$message.success(response.message || '编辑成功')
            }).catch(error => {
              this.$message.error(error.response.data['message'])
            })
          }
          this.libDialogShow = false
          await this.fetchData()
        }
      })
    },
    async deleteLib(item) {
      const data = [item.id]
      await this.deleteSubmit(data)
    },
    async deleteSubmit(data) {
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        lockScroll: false,
        type: 'warning'
      }).then(async() => {
        await apiCaselib.deleteCaselib(data).then(async response => {
          this.$message.success(response.message || '删除成功')
          await this.fetchData()
        })
      })
    },
    async followLib(item) {
      const data = {
        caselib_id: item.id,
        follow: !item.is_follow
      }
      await apiCaselib.followCaselib(data).then(response => {
        this.$message.success(response.message || '关注成功')
        this.fetchData()
      })
    },
    gotoCaselibDetail(item) {
      this.$router.push({
        path: '/functional/casemanage',
        query: {
          caselibId: item.id
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
